package neusoft.example.com.yuejian06.Model;

import neusoft.example.com.yuejian06.Bean.Activity;
import neusoft.example.com.yuejian06.PublicActivity;
import neusoft.example.com.yuejian06.RetrofitListener;
import neusoft.example.com.yuejian06.Service.ActivityService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ActivityModel {
    private Retrofit retrofit;
    private ActivityService service;
    private PublicActivity publicActivity;

    public ActivityModel(){
        publicActivity=new PublicActivity();
        retrofit=new Retrofit.Builder()
                .baseUrl(publicActivity.getIp()+"api/Activity/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service=retrofit.create(ActivityService.class);
    }
    public void  getActivity(String activity_id,final RetrofitListener<Activity> activitylistener){
        Call<Activity> call=service.getActivity(activity_id);
        call.enqueue(new Callback<Activity>(){

            @Override
            public void onResponse(Call<Activity> call, Response<Activity> response) {
                if(response.isSuccessful() && response!=null){
                    activitylistener.onResponse(response.body());
                }else{
                    activitylistener.onFail("服务器炸了！");//
                }
            }

            @Override
            public void onFailure(Call<Activity> call, Throwable t) {
                activitylistener .onFail(t.toString());
            }
        });

    }
}
